//
//  WJPHomeTableViewController.m
//  DuiTang--
//
//  Created by 万贾平 on 16/9/29.
//  Copyright © 2016年 jpw. All rights reserved.
//
#import "WJPHomeTableViewController.h"
#import <MJExtension/MJExtension.h>
#import "DWHomeItem.h"
#import <AFNetworking.h>
#import "DWHomeSmallCell.h"
#import "RGHomeLargeCell.h"
#import "RGHomeDetailController.h"
#import "HomeFirstCell.h"
#import "RGHomeLargeCellVM.h"
#import "RGOnceTool.h"
#define DTURL @"http://www.duitang.com/napi/ad/banner/list/?platform_name=iPhone%20OS&start=0&query_type=normal&__domain=www.duitang.com&app_version=6.3.2%20rv%3A168184&device_platform=iPhone7%2C2&locale=zh_CN&app_code=gandalf&platform_version=9.3.5&screen_height=667&device_name=Unknown%20iPhone&limit=0&ad_id=IGA009&screen_width=375"
@interface WJPHomeTableViewController () 
@property (nonatomic , strong ) NSMutableArray *homeItems;
@property (nonatomic , assign) CGFloat cellH;
@property (nonatomic , strong) NSMutableArray *homeVMs;
@property (nonatomic , strong) RGOnceTool *onceStr;
@end

@implementation WJPHomeTableViewController

-(NSMutableArray *)homeItems{
    if (!_homeItems) {
        NSMutableArray *arr = [NSMutableArray array];
        _homeItems = arr;
    }
    return _homeItems;
}
-(NSMutableArray *)homeVMs{
    if (!_homeVMs) {
        NSMutableArray *arr = [NSMutableArray array];
        _homeVMs = arr;
    }
    return _homeVMs;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUpNaviBar];
    //注册大图Cell 和小图Cell
    self.onceStr = [RGOnceTool shareOnceTool];
    self.onceStr = @"1111";
    self.tableView.autoresizingMask = NO;
    [self.tableView registerNib:[UINib nibWithNibName:@"DWHomeSmallCell" bundle:nil] forCellReuseIdentifier:@"small"];
    [self.tableView registerClass:[RGHomeLargeCell class] forCellReuseIdentifier:@"large"];
    [self.tableView registerNib:[UINib nibWithNibName:@"HomeFirstCell" bundle:nil] forCellReuseIdentifier:@"first"];
    [self loadData];

}
-(void)setUpNaviBar{
    self.view.backgroundColor = [UIColor lightGrayColor];
    self.navigationItem.title = @"每日精选";
    self.navigationItem.rightBarButtonItem = [UIBarButtonItem withImageName:@"nav_icon_add"];
    self.tableView.backgroundColor = [UIColor lightGrayColor];
}
-(void)loadData{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager GET:DTURL parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary *  _Nullable responseObject) {
        NSArray *dictArr = responseObject[@"data"][@"object_list"];
        self.homeItems = [DWHomeItem mj_objectArrayWithKeyValuesArray:dictArr];
        for (DWHomeItem *item in self.homeItems) {
            RGHomeLargeCellVM *vm = [[RGHomeLargeCellVM alloc] init];
            vm.item = item;
            [self.homeVMs addObject:vm];
        }
        
        
        [self.tableView reloadData];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@",error);
    }];
}
#pragma - mark tableViewCell的相关设定
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.homeItems.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    DWHomeItem *item = self.homeItems[indexPath.row];
    RGHomeLargeCellVM *vm = self.homeVMs[indexPath.row];
    if (indexPath.row == 0) {
        HomeFirstCell *cell =[tableView dequeueReusableCellWithIdentifier:@"first" forIndexPath:indexPath];
        cell.item = item;
        cell.selectionStyle = UIAccessibilityTraitNone;
        return cell;
    }
    if ([item.style isEqualToString:@"small"]) {
        DWHomeSmallCell *cell = [tableView dequeueReusableCellWithIdentifier:@"small" forIndexPath:indexPath];
        cell.item = item;
        cell.selectionStyle = UIAccessibilityTraitNone;
        return cell;
    }else{
        RGHomeLargeCell *cell = [tableView dequeueReusableCellWithIdentifier:@"large" forIndexPath:indexPath];
        cell.vm = vm;
        cell.selectionStyle = UIAccessibilityTraitNone;
        return cell;
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    DWHomeItem *item = self.homeItems[indexPath.row];
    RGHomeLargeCellVM *vm = self.homeVMs[indexPath.row];
    if ([item.style isEqualToString:@"small"])
        return 150 + 7;
    else if(indexPath.row == 0)
        return 227 + 7;
    else
        return vm.cellH + 7;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    DWHomeItem *item = self.homeItems[indexPath.row];
    RGHomeDetailController *detailVC = [[RGHomeDetailController alloc] init];
    detailVC.url = [NSURL URLWithString:item.target];
    [self.navigationController pushViewController:detailVC animated:YES];
}

@end
